package com.njth.util;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class InitContext extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2928000329528065243L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter out=resp.getWriter();
		out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\" >");
		out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
		out.println("<head>");
		out.println("<title>Sleep</title>");
		out.println("</head><body>");
		out.println("<h1>I am tired, and want to sleep!</h1>");
		out.println("</body><html>");
		System.out.println("警报：IP为"+req.getRemoteAddr()+"的主机试图访问xhtml文件！");
	}

	@Override
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		super.init(config);
		System.out.println("******************************初始化应用程序参数**************************************");
		Config.createInstance(config.getServletContext());
		
    	
		System.out.println("******************************初始化应用程序参数**************************************");
		
		/*try {

            Context ctx = new InitialContext();

            DataSource ds = (DataSource) ctx.lookup("jdbc/_MySqlDataSource");

            Connection conn = ds.getConnection();
            
            PreparedStatement pstmt=conn.prepareStatement("select id,name from tbg_user");
            ResultSet rs=pstmt.executeQuery();
            while(rs.next()){
            	String name=rs.getString("name");
            	long id=rs.getLong("id");
            	System.out.println("%%%%%%%%%%%---"+id+"-"+name);
            }
            
            System.out.println("********JNDI---"+conn);

         } catch (Exception ex) {

            ex.printStackTrace();

        }*/
	}

}
